import math
import time

start_time = int(round(time.time() * 1000))
total = 0
facs = [math.factorial(x) for x in range(10)]
def getrange():
	x = 1
	digits = 0
	sum = 0
	while True:
		sum = sum + facs[9]
		digits = digits + (9 * 10**x)
		if digits > sum:
			return sum
		x = x + 1

for num in range(3,getrange()+1):
	sum = 0
	for d in str(num):
		sum = sum + facs[int(d)]
	if sum == num:
		total = total + num
print(facs)
print("Total: "+str(total)+", Time: "+str((int(round(time.time() * 1000))-start_time)/1000)+"seconds")
'''x = 1
while True:
	sum = 0
	for d in str(x):
		sum = sum + facs[int(d)]
	if sum == x:
		total = total + x
		print(x)
	x = x + 1'''